package com.example.bbs.controller.config;

import com.example.bbs.service.impl.Quartzservice;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author jiajiazi
 * @version 1.0
 * @date 2021/6/14 12:18
 */

//@Configuration
public class QuartzConfig {
    /**
     * 定时任务
     * @return
     */
    @Bean
    public JobDetail jobDetail() {
        return JobBuilder.newJob(Quartzservice.class)
                .withIdentity("jobDetail")
                .storeDurably()
                .build();
    }

    /**
     * 触发器，每间隔一段时间触发定时任务
     * @param jobDetail 具体执行的定时任务
     * @return
     */
    @Bean
    public Trigger jobTrigger(@Qualifier("jobDetail") JobDetail jobDetail) {
        ScheduleBuilder scheduleBuilder = SimpleScheduleBuilder
                .simpleSchedule()
                .withIntervalInMinutes(1) // 定时任务间隔时间
                .repeatForever();
        return TriggerBuilder.newTrigger()
                .forJob(jobDetail)
                .withIdentity("jobTrigger")
                .withSchedule(scheduleBuilder)
                .build();
    }
}
